package cn.gavin.service.impl;

import cn.gavin.domain.Department;
import cn.gavin.mapper.DepartmentMapper;
import cn.gavin.qo.QueryObject;
import cn.gavin.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void deleteById(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Department department) {
        departmentMapper.insert(department);
    }

    @Override
    public Department findById(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> listAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public void update(Department department) {
        departmentMapper.updateByPrimaryKey(department);
    }

    @Override
    public PageInfo<Department> query(QueryObject qo) {
        // 以下分页插件帮我们做了
        // 不需要我们写 count 语句，它会自动生成并执行
        // limit 是插件自动计算参数并拼接上去，QueryObject 类里的 getStart 不用写
        // 提供一个 PageInfo 类 ，用于封装分页相关数据，不需要自己写 PageResult 类
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); // 对下一句 sql 进行分页，传入当前页和每页显示数量
        List<Department> departments = departmentMapper.dataList(qo);
        return new PageInfo<>(departments);
    }
}
